package com.dingqb.myapplication;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dingqb.myapplication.paint.MoveFromFinger;
import com.dingqb.myapplication.paint.MoveFromFingerbejizer;
import com.dingqb.myapplication.view.MyTextView;

public class PropertyActivity extends AppCompatActivity {
    private static final String TAG = "PropertyActivity";
    TextView tv_property;
    TextView tv_ofobj;
    TextView tv_objanimator;
    MyTextView tv_myview;
    ImageView iv_item1;
    ImageView iv_item2;
    ImageView iv_item3;
    ImageView iv_item4;
    ImageView iv_item5;
    ImageView iv_menu;
    ImageView iv_qq;
    boolean isOpen = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_property);
        tv_property = findViewById(R.id.tv_property);
        tv_ofobj = findViewById(R.id.tv_ofobj);
        tv_objanimator = findViewById(R.id.tv_objanimator);
        tv_myview = (MyTextView) findViewById(R.id.tv_myview);
        iv_item1 = findViewById(R.id.iv_item1);
        iv_item2 = findViewById(R.id.iv_item2);
        iv_item3 = findViewById(R.id.iv_item3);
        iv_item4 = findViewById(R.id.iv_item4);
        iv_item5 = findViewById(R.id.iv_item5);
        iv_menu = findViewById(R.id.iv_menu);
        iv_qq = findViewById(R.id.iv_qq);
        bitmaphw();
        iv_item1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(PropertyActivity.this, "" + v.toString(), Toast.LENGTH_SHORT).show();
            }
        });
        iv_menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isOpen) {
                    isOpen = true;
                    int count = 4;
                    double degree = Math.toRadians(90) / count;
                    open(iv_item1, degree * 0, 300);
                    open(iv_item2, degree * 1, 300);
                    open(iv_item3, degree * 2, 300);
                    open(iv_item4, degree * 3, 300);
                    open(iv_item5, degree * 4, 300);
                } else {
                    isOpen = false;
                    int count = 3;
                    double degree = Math.toRadians(90) / count;
                    close(iv_item1, degree * 0, 300);
                    close(iv_item2, degree * 1, 300);
                    close(iv_item3, degree * 2, 300);
                    close(iv_item4, degree * 3, 300);
                    close(iv_item5, degree * 4, 300);
                }

            }
        });
        tv_property.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setPropertyAnim();
            }
        });
    }

    void open(View view, double degree, int radius) {

        int translateX = (int) (radius * Math.sin(degree));
        int translateY = (int) (radius * Math.cos(degree));
        AnimatorSet set = new AnimatorSet();
        set.playTogether(
                ObjectAnimator.ofFloat(view, "translationX", 0, translateX),
                ObjectAnimator.ofFloat(view, "translationY", 0, -translateY),
                ObjectAnimator.ofFloat(view, "alpha", 0, 1),
                ObjectAnimator.ofFloat(view, "scaleX", 0f, 0.5f),
                ObjectAnimator.ofFloat(view, "scaleY", 0f, 0.5f)
        );
        set.setDuration(2000);
        set.setInterpolator(new AccelerateDecelerateInterpolator());
        set.start();

    }

    void close(View view, double degree, int radius) {
        int translateX = (int) (radius * Math.sin(degree));
        int translateY = (int) (radius * Math.cos(degree));
        AnimatorSet set = new AnimatorSet();
        set.playTogether(
                ObjectAnimator.ofFloat(view, "translationX", translateX, 0),
                ObjectAnimator.ofFloat(view, "translationY", -translateY, 0),
                ObjectAnimator.ofFloat(view, "alpha", 1, 0),
                ObjectAnimator.ofFloat(view, "scaleX", 0.5f, 0),
                ObjectAnimator.ofFloat(view, "scaleY", 0.5f, 0)
        );
        set.setDuration(2000);
        set.setInterpolator(new DecelerateInterpolator());
        set.start();

    }


    void setPropertyAnim() {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            ValueAnimator animator = ValueAnimator.ofInt(0, 400);
            animator.setDuration(2000);
            animator.setRepeatCount(1);
//            animator.setRepeatCount(ValueAnimator.INFINITE);
//            animator.setRepeatMode(ValueAnimator.RESTART);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    int value = (int) animation.getAnimatedValue();
                    tv_property.layout(value, value, value + tv_property.getWidth(), value + tv_property.getHeight());
                    Log.e(TAG, "onAnimationUpdate: " + value);
                }
            });
            animator.start();
        }
    }

    public void ofObj(View view) {
        ValueAnimator valueAnimator = ValueAnimator.ofObject(new CarEmulator(), new Character('A'), new Character('Z'));
        valueAnimator.setDuration(2000);
        valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Character character = (Character) animation.getAnimatedValue();
                tv_ofobj.setText(character + "");
            }
        });
        valueAnimator.start();
    }

    public void objAnimator(View view) {
        ObjectAnimator objectAnimator = null;
//        objectAnimator = ObjectAnimator.ofFloat(tv_objanimator, "rotation", 0, 360, -60);
//        objectAnimator = ObjectAnimator.ofFloat(tv_objanimator, "rotationX", 0, 360, -60);
//        objectAnimator = ObjectAnimator.ofFloat(tv_objanimator, "translationX", 0, 480, 240);
//        objectAnimator = ObjectAnimator.ofFloat(tv_objanimator, "scaleX", 1f, 1.5f, 2f, 1.5f,1);
        objectAnimator = ObjectAnimator.ofFloat(tv_objanimator, "scaleY", 1f, 1.5f, 2f, 1.5f, 1);
        objectAnimator.setDuration(3000);
        objectAnimator.setInterpolator(new LinearInterpolator());
        objectAnimator.start();
    }

    public void myView(View view) {
        ObjectAnimator animator = ObjectAnimator.ofInt(tv_myview, "xy", 2, 3, 2, 1);
        animator.setDuration(2000);
        animator.start();
    }


    public void objAnimatorXml(View view) {

        ObjectAnimator animator = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.anim_objanim);
        animator.setTarget(view);
        animator.start();
    }

    public void objAnimatorXmlSet(View view) {
        AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.anim_og_set);
        set.setTarget(view);
        set.start();
    }

    public void keyFrame(View view) {
        Keyframe keyframe1 = Keyframe.ofFloat(0.1f, 100f);
        Keyframe keyframe2 = Keyframe.ofFloat(0.25f, 200f);
        Keyframe keyframe3 = Keyframe.ofFloat(0.5f, 100f);
        Keyframe keyframe4 = Keyframe.ofFloat(1.0f, 0f);

        Keyframe keyframe11 = Keyframe.ofFloat(0.1f, 0.1f);
        Keyframe keyframe22 = Keyframe.ofFloat(0.25f, 0.25f);
        Keyframe keyframe33 = Keyframe.ofFloat(0.5f, 0.5f);
        Keyframe keyframe44 = Keyframe.ofFloat(1.0f, 1.0f);
        PropertyValuesHolder holder1 = PropertyValuesHolder.ofKeyframe("translationY", keyframe1, keyframe2, keyframe3, keyframe4);
        PropertyValuesHolder holder2 = PropertyValuesHolder.ofKeyframe("alpha", keyframe11, keyframe22, keyframe33, keyframe44);
        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, holder1, holder2);
        animator.setDuration(2000);
        animator.start();
    }

    public void viewProprerty(View view) {
        view.animate().alpha(0.2f).alphaBy(1.0f)
//                .rotationX(360).rotationXBy(360)
                .translationX(100).translationXBy(100)
                .scaleX(1.5f).scaleXBy(1f)
                .scaleY(2).scaleYBy(1)
                .setDuration(2000)
                .setUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {

                    }
                })
                .setListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                })
        ;
    }

    MoveFromFinger view_painter;
    MoveFromFingerbejizer view_painter_beijizer;

    public void clearLine(View view) {
        view_painter = findViewById(R.id.view_painter);
        view_painter_beijizer = findViewById(R.id.view_painter_beijizer);
        view_painter.reset();
        view_painter_beijizer.reset();

    }

    class CarEmulator implements TypeEvaluator<Character> {
        @Override
        public Character evaluate(float fraction, Character startValue, Character endValue) {
            int start = startValue;
            int end = endValue;
            int res = (int) (start + fraction * (end - start));
            char result = (char) res;
            return result;
        }
    }

    void animatorSet() {
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playSequentially();
        animatorSet.playTogether();
        //
        AnimatorSet.Builder builder = animatorSet.play(null);
        builder.with(null);
        builder.after(null);
        builder.after(null);

    }

    void bitmaphw() {
        //绘制底部icon
        Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.qq);
        float h = bitmap1.getHeight();
        float w = bitmap1.getWidth();
        Log.e(TAG, "bitmaphw: width==" + w);
        Log.e(TAG, "bitmaphw: height==" + h);
        if (!bitmap1.isRecycled()) {

            bitmap1.recycle();
        }
        iv_qq.getViewTreeObserver().addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
            @Override
            public void onDraw() {
                int h = iv_qq.getHeight();
                int w = iv_qq.getWidth();
                Log.e(TAG, "addOnDrawListener: width==" + w);
                Log.e(TAG, "addOnDrawListener: height==" + h);
                // iv_qq.getViewTreeObserver().removeOnDrawListener(this);
            }
        });

        iv_qq.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int h = iv_qq.getHeight();
                int w = iv_qq.getWidth();
                Log.e(TAG, "addOnGlobalLayoutListener: width==" + w);
                Log.e(TAG, "addOnGlobalLayoutListener: height==" + h);
                // iv_qq.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
    }
}
